package com.wondream.myframework.app.common.view;

import com.google.common.collect.Maps;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Locale;
import java.util.Map;

/**
 * 视图解析器
 */
public class CfssViewResolver implements ViewResolver, Ordered {

    @Resource
    private ApplicationContext applicationContext;

    private Map<String, View> viewMap = Maps.newHashMap();

    @PostConstruct
    private void init(){
        Map<String, CfssAbstractView> beanMap = applicationContext.getBeansOfType(CfssAbstractView.class);
        if (null==beanMap) {
            return;
        }
        beanMap.keySet().forEach(key -> {
            CfssAbstractView cfssAbstractView = beanMap.get(key);
            viewMap.put(cfssAbstractView.getView(), cfssAbstractView);
        });
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }

    @Override
    public View resolveViewName(String viewName, Locale locale) throws Exception {
        return viewMap.get(viewName);
    }
}
